package com.cpo.dao;

import java.awt.peer.LightweightPeer;
import java.util.List;

import org.hibernate.Query;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.cpo.domain.User;
import com.cpo.interfaces.IUserRegistration;

@Repository
@Transactional
public class UserRegistrationDao implements IUserRegistration {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public User getById(int id){
		
		return (User) sessionFactory.getCurrentSession().get(User.class, id);
	}
	@SuppressWarnings("unchecked")
	public List<User> searchUser(String firstName){
		
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
		criteria.add(Restrictions.ilike("firstName", firstName+"%"));
		return criteria.list();
	}
	@SuppressWarnings("unchecked")
	public List<User> getAllUsers(){
		
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
		/*Query query =  sessionFactory.getCurrentSession().createQuery("from user_details");*/
		
		//int size = criteria.list().size();
		return criteria.list();
	}
	
	public int save(User user) {
		
		return (Integer) sessionFactory.getCurrentSession().save(user);
		}
		
	
	
	public void update(User user){
	
		sessionFactory.getCurrentSession().merge(user);
		
	}
	public void delete(int id){
		
		User user = getById(id);
		sessionFactory.getCurrentSession().delete(user);
	}
	
	public List<User> getUserByEmpId(String empid){
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
		criteria.add(Restrictions.ilike("empid", empid));
		return criteria.list();
	}
}